package com.controller;

import com.aop.SystemLog;
import com.aop.SystemMail;
import com.dto.BaseDto;
import com.entity.Condition;
import com.entity.Member;
import com.page.PageInfo;
import com.service.MemberService;
import com.utils.TimeDateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/memberController")
public class MemberController extends BaseController {
   @Resource
    MemberService memberService;

    @RequiresPermissions("member:members")
    @GetMapping("/members")
    //@ResponseBody
    @SystemLog(methods = "查询全部",module = "会员管理")
    public BaseDto getAll() throws Exception{

        return memberService.getAll();
    }

    //根据主键查询,可以使用占位符来声明变量{}占位 mid就是参数值,通过 @PathVariable 绑定指定变量赋值
    @GetMapping("/member/{mno}/{state}")
    public ModelAndView getMemberByMno(@PathVariable("mno") String mno, @PathVariable("state") String state)throws Exception{
        //System.out.println(mno);
        BaseDto dto = memberService.getMemberByMno(mno);
        //下一面一句等价于 request.getDispatcherRequest.forward + request.setAttibute("dto",dto)
        ModelAndView mv = new ModelAndView();
        //同样是按主键查询 可能进 个人信息页面 也可能进 修改个人信息页面
        switch (state){
            case "update":  mv.setViewName("/member/update");break;
            default:mv.setViewName("/member/info");
        }
        mv.addObject("dto",dto);

        return mv;
    }

    @GetMapping("/members/page")
    public ModelAndView getMembersByPage(PageInfo pageInfo) {
        String cacheId = "MembersPage"+pageInfo.getCurrPage();
        BaseDto dto = memberService.getMembersByPage(pageInfo,cacheId);
        //下一面一句等价于 request.getDispatcherRequest.forward + request.setAttibute("dto",dto)
        ModelAndView modelAndView = new ModelAndView("/member/list", "dto", dto);
        modelAndView.addObject("pageInfo", pageInfo);

        return modelAndView;
    }

    @GetMapping("/members/page/json")
    @RequiresPermissions("member:pagejson")
    @ResponseBody
    public BaseDto getMembersByPageJSON(PageInfo pageInfo) {
        int currPage = pageInfo.getCurrPage();
        BaseDto dto = null;
        if(currPage <= 2) {
            String cacheId = "MembersPage" + currPage;
            dto  = memberService.getMembersByPage(pageInfo, cacheId);
            //下一面一句等价于 request.getDispatcherRequest.forward + request.setAttibute("dto",dto)
        }else{
            dto  = memberService.getMembersByPage(pageInfo);
        }
        return dto;
    }

    /**
     * 根据主键查询对象带全部关联
     * @param mno
     * @return
     */
    @GetMapping("/member/all/json/{mno}")
    @ResponseBody
    public BaseDto getMembersByPageJSON(@PathVariable("mno") String mno) throws Exception {
        BaseDto dto = memberService.memberByMnoWithAll(mno);
        return dto;
    }

    @PostMapping("/members/search/{currPage}")
    @ResponseBody
    public BaseDto searchMembersByPage(@PathVariable("currPage") String currPage, @RequestBody List<Condition> conditions) throws ParseException,NumberFormatException{
        PageInfo pageInfo = new PageInfo();
        int realCurrPage = Integer
        .parseInt(Optional.ofNullable(currPage).orElse("1"));//如果为kong 默认为1 如果非法字符抛出集中处理
        pageInfo.setCurrPage(realCurrPage);
        BaseDto dto = memberService.searchMembersByPage(pageInfo,conditions);
        //下一面一句等价于 request.getDispatcherRequest.forward + request.setAttibute("dto",dto)
        dto.setObj(pageInfo);//dto带上分页信息
        return dto;
    }


    //@RequestMapping(value="/member",method = RequestMethod.POST)
    //方法 url名字相同 但请求方式不同 用不通电的方法来相应  restful
    @PostMapping("/member")
    @ResponseBody
    @SystemMail(op = "Member:reg",queueName = "lg.user.reg",module = "会员管理",methods = "会员注册")
    public BaseDto add(@Valid Member member) throws Exception {
        member.setMno(TimeDateUtils.getCurrDateAndTimeString());//设置主键  设置注册日期
        member.setMjoinday(TimeDateUtils.getCurrDate());
        return memberService.add(member) ;
      }
    @PutMapping("/member")
    @ResponseBody
    public BaseDto update(Member member)throws Exception {
        System.out.println("1111");
        return memberService.update(member) ;
    }

    //@RequestMapping(value="/member",method = RequestMethod.GET)
    @GetMapping("/member")
    @ResponseBody
    public String getById(){
        return "getById1";
    }
    //get 查询  post add  put 修改  patch 部分修改   delete 删除

    @DeleteMapping("/member")
    @ResponseBody
    public String delete(){return "delete";}
    @PatchMapping("/member")
    @ResponseBody
    public String patch(){return "patch";}

    /**
     *
     */
    @PostMapping("/member/buyrecord")

    public ModelAndView memberBuyPrograme(Member member, String prno) throws Exception {
        System.out.println(prno);
        BaseDto dto = memberService.buyProgram(member,prno);
        return new ModelAndView("member/buyProgramresult","dto",dto);
    }
}
